package com.canyou.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.joda.money.Money;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spi.service.contexts.SecurityContextBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;

/**
 * swagger2的配置文件
 *
 * @author cgz
 * @date 2018-12-11
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {

    @Bean
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("v1.0")
                .directModelSubstitute(Money.class, BigDecimal.class)
                .apiInfo(new ApiInfoBuilder()
                        .contact(new Contact("canyou", "https://www.canyousoftware.com/", "tg@canyou.cn"))
                        .description("最小系统API文档")
                        .title("最小系统API文档")
                        .version("1.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.canyou"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Arrays.asList(securityScheme()))
                .securityContexts(Arrays.asList(securityContext()));
    }

    private final static String AUTH_PATH = "/oauth/token";
    private final static String CLIENT_ID = "admin-web";
    private final static String CLIENT_SECRET = "4402b06a67334d769fed712453284dae";

    private AuthorizationScope[] scopes() {
        AuthorizationScope[] scopes = {
                new AuthorizationScope("all", "all")
        };
        return scopes;
    }

    private SecurityScheme securityScheme() {
        return new OAuthBuilder()
                .name("oauth2")
                .grantTypes(Arrays.asList(new ResourceOwnerPasswordCredentialsGrant(AUTH_PATH)))
                .scopes(Arrays.asList(scopes()))
                .build();

    }

    private SecurityContext securityContext() {
        return new SecurityContextBuilder()
                .securityReferences(Collections.singletonList(new SecurityReference("oauth2", scopes())))
                // .forPaths(PathSelectors.any())
                .operationSelector(operationContext -> true)
                .build();
    }

    @Bean
    public SecurityConfiguration security() {
        return SecurityConfigurationBuilder.builder()
                .clientId(CLIENT_ID)
                .clientSecret(CLIENT_SECRET)
                .scopeSeparator(" ")
                .useBasicAuthenticationWithAccessCodeGrant(true)
                .appName("最小系统")
                .build();
    }

}